#include "mainwindow.h"

#include <QApplication>
#include <QtWebSockets/QWebSocketServer>
#include <QWebSocket>
#include <QFile>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebSocketServer server("Server", QWebSocketServer::NonSecureMode);
        if (!server.listen(QHostAddress::Any, 1234)) {
            qDebug() << "Failed to start server.";
            return 1;
        }

        QObject::connect(&server, &QWebSocketServer::newConnection, [&]() {
            qDebug() << "New Connection!";

            QWebSocket *socket = server.nextPendingConnection();
                QObject::connect(socket, &QWebSocket::binaryMessageReceived, [&](const QByteArray& message) {
                    QString savePath = "/path/to/save/file.txt"; // 要保存接收文件的路径
                    QFile file(savePath);
                    if (file.open(QIODevice::WriteOnly)) {
                        file.write(message); // 将接收到的数据写入文件
                        file.close();
                        qDebug() << "File received and saved:" << savePath;
                    } else {
                        qDebug() << "Failed to open file for writing.";
                    }
                    socket->close();
                    socket->deleteLater();
                    //QCoreApplication::quit();
                });
            });

    //MainWindow w;
    //w.show();
    return a.exec();
}
